package com.cms.entity;

import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;

import com.cms.entity.base.BaseConfig;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

/**
 * Entity - 配置
 * 
 * 
 * 
 */
@SuppressWarnings("serial")
public class Config extends BaseConfig<Config> {
	
    /** 缓存名称 */
    public static final String CACHE_NAME = "config";
    
	/**
	 * 查找配置
	 * 
	 * @param name
	 *           name
	 * @return 配置
	 */
	public Config findByName(String name,Long siteId){
		return findFirstByCache(Config.CACHE_NAME,DigestUtils.md5Hex(name+siteId),"select * from cms_config where siteId=? and name = ?",siteId,name);
	}
	
   /**
     * 判断名称是否存在
     * 
     * @param name
     *            名称
     * @return 名称是否存在
     */
    public boolean nameExists(String name,Long siteId) {
        if (StrKit.isBlank(name)) {
            return false;
        }
        Long count = Db.queryLong("select count(1) from cms_config where siteId=? and name = ?",siteId,name);
        return count > 0;
    }
	
	/**
	 * 查找所有配置
	 * 
	 * @return 所有配置
	 */
	public List<Config> findAll(Long siteId){
		return findByCache(Config.CACHE_NAME,Config.CACHE_NAME+siteId,"select * from cms_config where siteId="+siteId);
	}
	
   /**
     * 查找配置
     * 
     * @return 配置
     */
    public List<Config> findList(Boolean isSystem,Long siteId){
        String filterSql = " and siteId="+siteId;
        if(isSystem!=null){
            filterSql+= " and isSystem="+isSystem;
        }
        return find("select * from cms_config where 1=1 "+filterSql);
    }
}
